package cn.libinal.scloud.base.controller;

import cn.libinal.scloud.base.common.dto.DtoMapper;
import cn.libinal.scloud.base.common.dto.RoleDto;
import cn.libinal.scloud.base.common.web.ResultJson;
import cn.libinal.scloud.base.entity.Role;
import cn.libinal.scloud.base.service.RoleService;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @author libinal
 * created at 2018/11/6 20:34
 */
@RestController
@RequestMapping("role")
public class RoleController {
    private final RoleService roleService;
    private final DtoMapper dtoMapper;

    public RoleController(RoleService roleService, DtoMapper dtoMapper) {
        this.roleService = roleService;
        this.dtoMapper = dtoMapper;
    }

    @PostMapping(name = "新增角色", path = "create")
    public ResultJson<Role> create(Role role) {
        return ResultJson.success(roleService.create(role));
    }

    @PostMapping(name = "关联菜单和权限", path = "relateMenusAndPermissions")
    public ResultJson<String> relateMenusAndPermissions(String roleId,
                                                        @RequestParam("menuIds[]") List<String> menuIds,
                                                        @RequestParam("permissionIds[]") List<String> permissionIds) {
        roleService.relateMenusAndPermissions(roleId, menuIds, permissionIds);
        return ResultJson.success("菜单分配成功");
    }

    @GetMapping(name = "根据用户ID获取角色", path = "getRoleByUserId")
    public ResultJson<Set<RoleDto>> getRoleByUserId(@RequestParam("userId") String userId) {
        Set<Role> roles = roleService.getRoleByUserId(userId);
        Set<RoleDto> roleDtos = dtoMapper.mapAsSet(roles, RoleDto.class);
        return ResultJson.success(roleDtos);
    }
}
